source("./lib/load_libraries.R")

R2009 <- pubmed.search("R+package[tiab]",list(mindate="2009/01/01",maxdate="2009/12/31",datetype="ppdt"),dest="temp",format="ris")

pubm
table(as.data.frame(R2009)$journal)

res <- EUtilsSummary("Kris Gevaert[au]")

summary(res)

fetch <- EUtilsGet(res)

plot.data <- data.frame(table(Title(fetch)))
plot.data <- plot.data[order(plot.data$Freq, decreasing=FALSE),]
plot.data$Var1 <- factor(plot.data$Var1, levels=unique(plot.data$Var1))
p <- ggplot(data=plot.data, aes(y=Var1, x=Freq))
p <- p + geom_point()
p



author <- "Kenny Helsens"

data.pubmed <- pubmed.get.df.by.Author(author)
data.gs <- gs.get.all(author=author)

d <- pubgs(data.pubmed=data.pubmed, data.gs=data.gs)
head(d)

journal <- "Proteomics"
data.pubmed <- pubmed.get.df.by.Journal(journal=journal, from=2010, to=2010)
describe(data.pubmed)
head(data.pubmed)
data.gs <- gs.get.all.by.journal(journal=journal,from=2010,to=2010)

d <- pubgs(data.pubmed=data.pubmed, data.gs=data.gs)
head(d, n=20)
nrow(d)


redisConnect()
redisSelect("qscore")
# redisFlushDB()
redisKeys()

key1 <- "molecular_cellular_proteomics_.2010.citation.list"
key2 <- "molecular_cellular_proteomics_.2010.pubmedid.list"

max <- redisLLen(key1)
# redisLIndex(key1, 1)
# redisLIndex(key2, 1)

hist((as.integer(c(redisLRange(key, start=0, max)))))

author <- "Kenny Helsens"
auth.df <- pubmed.get.df.by.Author(author=author, from=2010, to=2011)
library(Hmisc)
describe(auth.df)
i<-9
i <- 14

qscores <- c()
initRedis()
redisKeys()
for(i in 1:nrow(auth.df)){
          mypaper <- auth.df[i,]
          citkey <- makeRedisCitationKey(mypaper$journals, mypaper$years)
          pidkey <- makeRedisPIDKey(mypaper$journals, mypaper$years)
          
          max <- redisLLen(citkey)

          peercits <- redisLRange(citkey, start=0, max)
          peerpids <- redisLRange(pidkey, start=0, max)

          peercits <- as.integer(paste(unlist(peercits)))
          peerpids <- paste(unlist(peerpids))
          
          rank <- order(peercits, decreasing=TRUE)
          peercits <- peercits[rank]
          peerpids <- peerpids[rank]
          
          myrank <- which(peerpids == paste(mypaper$pid))
          mycit <- peercits[myrank]
          
          
          if(length(myrank) == 0){
                    cat(sprintf("Manuscript '%s' not indexed. (pid:%s)", mypaper$title, mypaper$pid))
                    qscores <- c(qscores, NA)
          }else{
                    qscore <- (1 - (myrank / length(peercits)))
                    qscores <- c(qscores, qscore)
          }
          
}

d <- data.frame(auth.df, qscores)

plot.data <- na.omit(d)

p <- ggplot(data=plot.data, aes(x=qscores, y=title, col=journals, shape=factor(years)))
p <- p + geom_point()
p <- p + scale_color_manual(values=cbPalette)
p <- p + xlim(c(0,1))
p




